//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SwaptionVolatilityCube.h"
using namespace Cephei::QL::Termstructures::Volatility::Swaption;
#include <gen/QL/Times/Period.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Indexes/SwapIndex.h>
#include <gen/QL/Termstructures/Volatility/Swaption/SwaptionVolatilityDiscrete.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
using namespace Cephei::QL::Indexes;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityCube::CSwaptionVolatilityCube (boost::shared_ptr<QuantLib::SwaptionVolatilityCube>& childNative, Object^ owner) : CSwaptionVolatilityDiscrete(CSwaptionVolatilityCube::typeid)
{
#ifdef HANDLE
	_phSwaptionVolatilityCube = NULL;
#endif
	_ppSwaptionVolatilityCube = &childNative;
    _ppSwaptionVolatilityDiscrete = new boost::shared_ptr<QuantLib::SwaptionVolatilityDiscrete> (boost::dynamic_pointer_cast<QuantLib::SwaptionVolatilityDiscrete> (*_ppSwaptionVolatilityCube));
}
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityCube::CSwaptionVolatilityCube (QuantLib::SwaptionVolatilityCube& childNative, Object^ owner) : CSwaptionVolatilityDiscrete(CSwaptionVolatilityCube::typeid)
{
#ifdef HANDLE
	_phSwaptionVolatilityCube = NULL;
#endif
	_ppSwaptionVolatilityCube = new boost::shared_ptr<QuantLib::SwaptionVolatilityCube> (&childNative);
    _ppSwaptionVolatilityDiscrete = new boost::shared_ptr<QuantLib::SwaptionVolatilityDiscrete> (boost::dynamic_pointer_cast<QuantLib::SwaptionVolatilityDiscrete> (*_ppSwaptionVolatilityCube));
    _SwaptionVolatilityCubeOwner = owner;
    _SwaptionVolatilityDiscreteOwner = owner;
}

Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityCube::CSwaptionVolatilityCube (CSwaptionVolatilityCube^ copy) : CSwaptionVolatilityDiscrete(CSwaptionVolatilityCube::typeid)
{
#ifdef HANDLE
	_phSwaptionVolatilityCube = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSwaptionVolatilityCube = new boost::shared_ptr<QuantLib::SwaptionVolatilityCube> (copy->GetShared());
        _ppSwaptionVolatilityDiscrete = new boost::shared_ptr<QuantLib::SwaptionVolatilityDiscrete> (boost::dynamic_pointer_cast<QuantLib::SwaptionVolatilityDiscrete> (*_ppSwaptionVolatilityCube));
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityCube::CSwaptionVolatilityCube (PLATFORM::Type^ t) : CSwaptionVolatilityDiscrete(CSwaptionVolatilityCube::typeid)
{
#ifdef HANDLE
	_phSwaptionVolatilityCube = NULL;
#endif
	if (!t->IsSubclassOf(CSwaptionVolatilityCube::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityCube::CSwaptionVolatilityCube (QuantLib::Handle<QuantLib::SwaptionVolatilityCube>& childNative, Object^ owner)  : CSwaptionVolatilityDiscrete(CSwaptionVolatilityCube::typeid)
{
	_phSwaptionVolatilityCube = &childNative;
	_ppSwaptionVolatilityCube = &static_cast<boost::shared_ptr<QuantLib::SwaptionVolatilityCube>>(childNative.currentLink());
    _ppSwaptionVolatilityDiscrete = new boost::shared_ptr<QuantLib::SwaptionVolatilityDiscrete> (boost::dynamic_pointer_cast<QuantLib::SwaptionVolatilityDiscrete> (*_ppSwaptionVolatilityCube));
    _SwaptionVolatilityCubeOwner = owner;
}
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityCube::CSwaptionVolatilityCube (QuantLib::Handle<QuantLib::SwaptionVolatilityCube> childNative)  : CSwaptionVolatilityDiscrete(CSwaptionVolatilityCube::typeid)
{
	_phSwaptionVolatilityCube = &childNative;
	_ppSwaptionVolatilityCube = &static_cast<boost::shared_ptr<QuantLib::SwaptionVolatilityCube>>(childNative.currentLink());
    _ppSwaptionVolatilityDiscrete = new boost::shared_ptr<QuantLib::SwaptionVolatilityDiscrete> (boost::dynamic_pointer_cast<QuantLib::SwaptionVolatilityDiscrete> (*_ppSwaptionVolatilityCube));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityCube::CSwaptionVolatilityCube (QuantLib::SwaptionVolatilityCube childNative)  : CSwaptionVolatilityDiscrete(CSwaptionVolatilityCube::typeid)
{
#ifdef HANDLE
	_phSwaptionVolatilityCube = NULL;
#endif
	_ppSwaptionVolatilityCube = new boost::shared_ptr<QuantLib::SwaptionVolatilityCube> (new QuantLib::SwaptionVolatilityCube (childNative));
    _ppSwaptionVolatilityDiscrete = new boost::shared_ptr<QuantLib::SwaptionVolatilityDiscrete> (boost::dynamic_pointer_cast<QuantLib::SwaptionVolatilityDiscrete> (*_ppSwaptionVolatilityCube));
}
#endif

Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityCube::~CSwaptionVolatilityCube ()
{
    if (_ppSwaptionVolatilityCube != NULL)
    {
	    delete _ppSwaptionVolatilityCube;
        _ppSwaptionVolatilityCube = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityCube::!CSwaptionVolatilityCube ()
{
    if (_ppSwaptionVolatilityCube != NULL)
    {
	    delete _ppSwaptionVolatilityCube;
    }
}
QuantLib::SwaptionVolatilityCube& Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityCube::GetReference ()
{
    if (_ppSwaptionVolatilityCube == NULL) throw REFNEW NativeNullException ();
	return **_ppSwaptionVolatilityCube;
}
boost::shared_ptr<QuantLib::SwaptionVolatilityCube>& Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityCube::GetShared ()
{
    if (_ppSwaptionVolatilityCube == NULL) throw REFNEW NativeNullException ();
	return *_ppSwaptionVolatilityCube;
}
QuantLib::SwaptionVolatilityCube* Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityCube::GetPointer ()
{
    if (_ppSwaptionVolatilityCube == NULL) throw REFNEW NativeNullException ();
	return &**_ppSwaptionVolatilityCube;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SwaptionVolatilityCube>& Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityCube::GetHandle ()
{
	if (_phSwaptionVolatilityCube == NULL)
	{
		_phSwaptionVolatilityCube = new Handle<QuantLib::SwaptionVolatilityCube> (*_ppSwaptionVolatilityCube);
	}
	return *_phSwaptionVolatilityCube;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityCube::HasNative () 
{
	return (_ppSwaptionVolatilityCube != NULL);
}

Double Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityCube::AtmStrike (Cephei::QL::Times::IPeriod^ optionTenor, Cephei::QL::Times::IPeriod^ swapTenor)
{
    CPeriod^ _CoptionTenor;
    CPeriod^ _CswapTenor;
    try
    {
        _CoptionTenor = safe_cast<CPeriod^> (optionTenor);
        _CoptionTenor->Lock();
        QuantLib::Period& _optionTenor = static_cast<QuantLib::Period&> (_CoptionTenor->GetReference ()); 
        _CswapTenor = safe_cast<CPeriod^> (swapTenor);
        _CswapTenor->Lock();
        QuantLib::Period& _swapTenor = static_cast<QuantLib::Period&> (_CswapTenor->GetReference ()); 
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppSwaptionVolatilityCube)->atmStrike ( _optionTenor,  _swapTenor );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CoptionTenor != nullptr) _CoptionTenor->Unlock();
        if (_CswapTenor != nullptr) _CswapTenor->Unlock();
    }
}
Double Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityCube::AtmStrike (DateTime optionDate, Cephei::QL::Times::IPeriod^ swapTenor)
{
    CPeriod^ _CswapTenor;
    try
    {
        QuantLib::Date _optionDate = (QuantLib::Date)ValueHelper::Convert (optionDate); //a
        _CswapTenor = safe_cast<CPeriod^> (swapTenor);
        _CswapTenor->Lock();
        QuantLib::Period& _swapTenor = static_cast<QuantLib::Period&> (_CswapTenor->GetReference ()); 
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppSwaptionVolatilityCube)->atmStrike ( _optionDate,  _swapTenor );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CswapTenor != nullptr) _CswapTenor->Unlock();
    }
}
Cephei::QL::Times::ICalendar^ Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityCube::Calendar::get ()
{
    try
    {
    	QuantLib::Calendar _rv = (QuantLib::Calendar)(*_ppSwaptionVolatilityCube)->calendar ( );   
        Cephei::QL::Times::CCalendar^ _nrv = REFNEW Cephei::QL::Times::CCalendar (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IDayCounter^ Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityCube::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter _rv = (QuantLib::DayCounter)(*_ppSwaptionVolatilityCube)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = REFNEW Cephei::QL::Times::CDayCounter (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityCube::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppSwaptionVolatilityCube)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityCube::MaxStrike::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppSwaptionVolatilityCube)->maxStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IPeriod^ Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityCube::MaxSwapTenor::get ()
{
    try
    {
    	QuantLib::Period& _rv = (QuantLib::Period&)(*_ppSwaptionVolatilityCube)->maxSwapTenor ( );   
        Cephei::QL::Times::CPeriod^ _nrv = REFNEW Cephei::QL::Times::CPeriod (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityCube::MaxTime::get ()
{
    try
    {
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppSwaptionVolatilityCube)->maxTime ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityCube::MinStrike::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppSwaptionVolatilityCube)->minStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityCube::ReferenceDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppSwaptionVolatilityCube)->referenceDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt32 Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityCube::SettlementDays::get ()
{
    try
    {
    	QuantLib::Natural _rv = (QuantLib::Natural)(*_ppSwaptionVolatilityCube)->settlementDays ( );   
        UInt32 _nrv = (UInt32)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

